package com.sugon.controller.tool;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.DataFilter;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.ToolsDemandPlanEntity;
import com.sugon.modules.base.model.vo.BaseApproveParamVO;
import com.sugon.modules.sparepart.consts.SparePartNeedsPlanProgress;
import com.sugon.modules.tools.model.vo.ToolsDemandPlanItemVO;
import com.sugon.modules.tools.service.IToolsNeedsPlanManager;
import com.sugon.service.ToolsDemandPlanItemService;
import com.sugon.service.ToolsDemandPlanService;
import com.sugon.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 备件需求计划Controller
 *
 * @author sugon
 * @date 2019-08-02 14:31:17
 */
@Controller
@RequestMapping("toolsdemandplan")
public class ToolsDemandPlanController {
    @Autowired
    private ToolsDemandPlanService toolsDemandPlanService;
    @Autowired
    private IToolsNeedsPlanManager toolsNeedsPlanManager;
    @Autowired
    private ToolsDemandPlanItemService toolsDemandPlanItemService;

    /**
     * 校验当前月份的需求是否已编制过
     * 如果编制过需要给用户提示
     */
    @RequestMapping("/checkOrganed")
    @ResponseBody
    public R checkOrganed(@RequestParam String organMonth) {
        // 获取登录用户所属车间
        String workshop = ShiroUtils.getWorkshop();
        if (StrUtil.isEmpty(workshop)) {
            return R.error("当前用户角色不允许编制需求计划");
        }
        boolean isExists = toolsDemandPlanService.isExists(workshop, organMonth);
        return R.ok().put("isExists", isExists ? 1 : 0);
    }

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("toolsdemandplan:list")
    @DataFilter(deptAlias = "APPLY_UNIT")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ToolsDemandPlanEntity> sparePartRequList = toolsDemandPlanService.queryList(query);
        int total = toolsDemandPlanService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sparePartRequList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("toolsdemandplan:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ToolsDemandPlanEntity sparePartRequ = toolsDemandPlanService.queryObject(id);

        return R.ok().put("sparePartRequ", sparePartRequ);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("toolsdemandplan:save")
    @ResponseBody
    public R save(@RequestBody ToolsDemandPlanEntity sparePartRequ) {
        return toolsDemandPlanService.save(sparePartRequ) > 0 ? R.ok() : R.error();
    }

    @RequestMapping("/commitApprove")
    @RequiresPermissions("toolsdemandplan:save")
    @ResponseBody
    public R commitApprove(@RequestParam String spareNeedsPlanId) {
        // 校验是否存在
        ToolsDemandPlanEntity sparePartRequ = toolsDemandPlanService.queryObject(spareNeedsPlanId);
        if (sparePartRequ == null) {
            return R.error("备件需求计划不存在，请确认!");
        }
        // 校验是否审核中
        int progress = sparePartRequ.getProgress();
        if (progress != SparePartNeedsPlanProgress.NEW && progress != SparePartNeedsPlanProgress.NOT_PASSED) {
            return R.error("备件需求计划审核中或已通过审核，无需重复提交!");
        }
        // 只可以提交自己编制的需求计划
        if (!sparePartRequ.getOrganUser().equals(ShiroUtils.getUserId())) {
            return R.error("只可以提交自己编制的需求计划!");
        }
        // 校验是否已填充内容e
        List<ToolsDemandPlanItemVO> list = toolsDemandPlanItemService.queryByPlanId(spareNeedsPlanId);
        if (CollectionUtil.isEmpty(list)) {
            return R.error("请在添加需求工装后再提交审核！");
        }
        return toolsNeedsPlanManager.commitApprove(sparePartRequ) ? R.ok() : R.error("提交失败，请稍后重试!");
    }

    /**
     * 车间授权人审批
     */
    @NonBusinessParamValid
    @RequestMapping("/deptCheck")
    @RequiresPermissions("toolsdemandplan:deptCheck")
    @ResponseBody
    public R deptCheck(@Validated @RequestBody BaseApproveParamVO approveParam, BindingResult bindingResult) {
        approveParam.setProcessItem(ProcessInfoEnum.SPA_BJXQ);
        return toolsNeedsPlanManager.workshopApprove(approveParam) ? R.ok() : R.error();
    }

    /**
     * PE备件计划专员
     */
    @NonBusinessParamValid
    @RequestMapping("/peCheck")
    @RequiresPermissions("toolsdemandplan:peCheck")
    @ResponseBody
    public R peCheck(@Validated @RequestBody BaseApproveParamVO approveParam, BindingResult bindingResult) {
        approveParam.setProcessItem(ProcessInfoEnum.SPA_BJXQ);
        return toolsNeedsPlanManager.peApprove(approveParam) ? R.ok() : R.error();
    }

    /**
     * PE设备技术人审核
     */
    @NonBusinessParamValid
    @RequestMapping("/pe2Check")
    @RequiresPermissions("toolsdemandplan:pe2Check")
    @ResponseBody
    public R pe2Check(@Validated @RequestBody BaseApproveParamVO approveParam, BindingResult bindingResult) {
        approveParam.setProcessItem(ProcessInfoEnum.SPA_BJXQ);
        return toolsNeedsPlanManager.pe2Approve(approveParam) ? R.ok() : R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("toolsdemandplan:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        toolsDemandPlanService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<ToolsDemandPlanEntity> list = toolsDemandPlanService.queryList(params);
        return R.ok().put("list", list);
    }

    @RequestMapping("/export")
    @ResponseBody
    @RequiresPermissions("toolsdemandplan:export")
    public R export(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        toolsDemandPlanService.export(params, response);
        return R.ok();
    }

}
